const useStorage = () => {
  const setItem = (key, value) => {
    localStorage.setItem(key, JSON.stringify(value))
  }

  const getItem = (key, defaultVal = '') => {
    const value = localStorage.getItem(key)
    if (!value) {
      return defaultVal
    }
    try {
      return JSON.parse(value)
    } catch (error) {
      return defaultVal
    }
  }

  const delItem = (key) => {
    localStorage.removeItem(key)
  }

  return {
    setItem,
    getItem,
    delItem,
  }
}

export default useStorage
